package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/2/2.
 */
public class FillFreeWayInfoAcitity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private TextView freeway_name,freeway_persionnum,freeway_carnum,freeway_platenum,freeway_address,freeway_paynum;

    private ImageView sex;

    private boolean flagsex = true;

    private int flag = 0;

    private TextView textViewBottom;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_freeway_fillinfo);

        init_view();

    }


    private void init_view() {

        (title_name = (TextView)findViewById(R.id.tv_title_name)).setText("填写信息");

        freeway_name = (TextView)findViewById(R.id.freeway_name) ;

        sex = (ImageView) findViewById(R.id.freeway_sex);

        sex.setOnClickListener(this);

        if(JTApplication.jtPreferences.readSex().equals("女")){

            sex.setImageResource(R.mipmap.icon_select_woman);

            flagsex = false;

        }

        (freeway_persionnum = (TextView)findViewById(R.id.freeway_persionnum)).setText(JTApplication.jtPreferences.readDriverNum());

        (freeway_carnum = (TextView)findViewById(R.id.freeway_carnum)).setText(JTApplication.jtPreferences.readDriverNum()) ;

        (freeway_platenum = (TextView)findViewById(R.id.freeway_platenum)).setText(JTApplication.jtPreferences.readPlateNum());

        freeway_address = (TextView)findViewById(R.id.freeway_address) ;

        freeway_paynum = (TextView)findViewById(R.id.freeway_paynum) ;

        textViewBottom = (TextView) findViewById(R.id.bottom_text);

        textViewBottom.setText("下一步");

        textViewBottom.setOnClickListener(this);

        freeway_persionnum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                flag = 0;

            }
        });

        freeway_carnum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                flag = 1;

            }
        });

        freeway_persionnum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(flag == 0){

                    freeway_carnum.setText(s.toString());

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        freeway_carnum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(flag == 1){

                    freeway_persionnum.setText(s.toString());

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.freeway_sex:

                if(flagsex){

                    sex.setImageResource(R.mipmap.icon_select_woman);

                }else{

                    sex.setImageResource(R.mipmap.icon_select_man);

                }
                flagsex = !flagsex;
                break;
            case R.id.bottom_text:

                if(!Tools.isName(freeway_name.getText().toString().trim())){

                    UtilToast.show(context,"请正确填写姓名");

                }else if(!Tools.personIdValidation(freeway_persionnum.getText().toString().trim())){

                    UtilToast.show(context,"请正确填写身份证号");

                }else if(!Tools.personIdValidation(freeway_carnum.getText().toString().trim())){

                    UtilToast.show(context,"请正确填写驾驶证号");

                }else if(!Tools.isPlateNO(freeway_platenum.getText().toString().trim())){

                    UtilToast.show(context,"请正确填写车牌号");

                }else if(freeway_address.getText().toString().trim().equals("")){

                    UtilToast.show(context,"请正确填写通讯地址");

                }else if(freeway_paynum.getText().toString().trim().equals("")){

                    UtilToast.show(context,"请正确填写沃支付账号");

                }else {

                    Intent intent = new Intent(FillFreeWayInfoAcitity.this, UpPictureActivity.class);

                    intent.putExtra("name",freeway_name.getText().toString().trim());

                    if(flagsex){

                        intent.putExtra("sex","男");

                    }else {

                        intent.putExtra("sex","女");

                    }

                    intent.putExtra("card_id",freeway_persionnum.getText().toString().trim());

                    intent.putExtra("plate_num",freeway_platenum.getText().toString().trim());

                    intent.putExtra("address",freeway_address.getText().toString().trim());

                    intent.putExtra("wo_account",freeway_paynum.getText().toString().trim());

                    startActivity(intent);

                }

                break;

        }
    }


}

